package com.fuxi;

/**
 * @ClassName SynchronizedDemo
 * @Description 注意下if和while判断  因为当调用wait()后 在唤醒 线程会从阻塞的位置重新执行，而不是从头开始
 * 第一种写法能够循环打印的原因是因为将i++放在成功获取锁并且满足打印条件后才执行的++操作
 * 如果将i++放到for循环的表达式中，须使用while来做调价判断
 * @Author admin
 * @Date 2021/6/2 15:11
 * @Version 1.0
 **/
public class SynchronizedDemo {
    
    private static Integer flag = 1;
    
    public static void main(String[] args) {
    
        Object lock = new Object();
    
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2;) {
                    synchronized (lock){
                        if(flag == 1){
                            System.out.println("AAA");
                            flag = 2;
                            i++;
                            lock.notifyAll();
                        }else{
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2;) {
                    synchronized (lock){
                        if(flag == 2){
                            System.out.println("BBB");
                            flag = 3;
                            i++;
                            lock.notifyAll();
                        }else{
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });

        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2;) {
                    synchronized (lock){
                        if(flag == 3){
                            System.out.println("CCC");
                            flag = 1;
                            i++;
                            lock.notifyAll();
                        }else{
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        
        
//        Thread threadA = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 2; i++) {
//                    synchronized (lock){
//                        while (flag != 1){
//                            try {
//                                lock.wait();
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                        System.out.println("AAA");
//                        flag = 2;
//                        lock.notifyAll();
//                    }
//                }
//            }
//        });
//
//        Thread threadB = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 2; i++) {
//                    synchronized (lock){
//                        while (flag != 2){
//                            try {
//                                lock.wait();
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                        System.out.println("BBB");
//                        flag = 3;
//                        lock.notifyAll();
//                    }
//                }
//            }
//        });
//
//        Thread threadC = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 2; i++) {
//                    synchronized (lock){
//                        while (flag != 3){
//                            try {
//                                lock.wait();
//                            } catch (InterruptedException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                        System.out.println("CCC");
//                        flag = 1;
//                        lock.notifyAll();
//                    }
//                }
//            }
//        });
        
        
        threadA.start();
        threadB.start();
        threadC.start();
        
    }
    
}
